package com.ysd.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.modules.sys.dao.TopicTypeDao;
import com.ysd.modules.sys.entity.TopicType;
import com.ysd.modules.sys.model.StatusReq;
import com.ysd.modules.sys.model.TopicResp;
import com.ysd.modules.sys.service.TopicTypeService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dapeng
 * @since 2021-07-15
 */
@Service
public class TopicTypeServiceImpl extends ServiceImpl<TopicTypeDao, TopicType> implements TopicTypeService {

    @Override
    public IPage<TopicType> listSearch(Map<String, Object> params) {
        QueryWrapper wrapper = new QueryWrapper();
        Integer page = Integer.parseInt(params.get("page").toString());
        Integer limit = Integer.parseInt(params.get("limit").toString());

        Page<TopicType> currPage = new Page<>();
        currPage.setCurrent(page);
        currPage.setSize(limit);

        Object name = params.get("name");
        wrapper.like("name", name == null ? "" : name);

        wrapper.ge("status", 0);
        IPage<TopicType> iPage = this.baseMapper.selectPage(currPage, wrapper);
        return iPage;
    }

    @Override
    public void statusModify(StatusReq req) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.in("id", req.getIds());
        updateWrapper.set("status", req.getStatus());
        this.update(updateWrapper);
    }

    @Override
    public List<TopicResp> listSearchByIds(List<TopicResp> topicResps) {
        List<Integer> ids = topicResps.stream().map(TopicResp::getTypeId).collect(Collectors.toList());

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.in("id", ids);
        List<TopicType> list = this.baseMapper.selectList(wrapper);

        for (TopicResp topicResp : topicResps) {
            for (TopicType type : list) {
                if (topicResp.getTypeId() == type.getId()) {
                    topicResp.setTypeName(type.getName());
                }
            }
        }
        return topicResps;
    }
}
